<template>
  <section class="todoapp">
    <TodoHeader @add="addFn" :arr="list"></TodoHeader>
    <TodoMain :arr="showArr" @dele="deleteFn"></TodoMain>
    <TodoFooter
      :arr="isSel"
      @change="changeFn"
      :leng="showArr.length"
      @clear="clearFn"
    ></TodoFooter>
  </section>
</template>

<script>
import "./styles/base.css";
import "./styles/index.css";

import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";

export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: [
        { id: 100, name: "吃饭", isDone: true },
        { id: 201, name: "睡觉", isDone: false },
        { id: 103, name: "打豆豆", isDone: true },
      ],
      isSel: "all",
    };
  },
  computed: {
    showArr() {
      if (this.isSel === "yes") {
        return this.list.filter((item) => item.isDone);
      } else if (this.isSel === "no") {
        return this.list.filter((item) => item.isDone === false);
      } else {
        return this.list;
      }
    },
  },
  methods: {
    addFn(taskName) {
      this.list.push({
        id: this.list.length ? this.list.at(-1).id + 1 : 100,
        name: taskName,
        isDone: false,
      });
    },
    deleteFn(inde) {
      return (this.list = this.list.filter((item, index) => index !== inde));
    },
    changeFn(type) {
      this.isSel = type;
    },
    clearFn() {
      return (this.list = this.list.filter((item) => item.isDone === false));
    },
  },
};
</script>